/*
 * @Author: your name
 * @Date: 2022-05-04 17:47:32
 * @LastEditTime: 2022-05-07 23:14:38
 * @LastEditors: zhouyanxin123 2434987548@qq.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \二阶段项目\gitcode\js\user.js
 */


pages();
producePage(guangGaoArr);

pagenumerevent("#pagUp");
pagenumerevent("#pagDow");
//渲染页面
function pages(guangGaoArr,pageSize,currentPage){
    $('#content').html('');
    pageSize=5;
    let start = (currentPage - 1)*pageSize;
    let end = currentPage*pageSize;
    for(let i=start;i<end && i<guangGaoArr.length;i++){
        $('#content').append(`<tr class='line'>
                <td><input type="checkbox" data-id="${guangGaoArr[i].id}" class='choose'></td>
                <td>${guangGaoArr[i].id}</td>
                <td>${guangGaoArr[i].guanggaomingcheng}</td>
                <td>${guangGaoArr[i].guanggaoquanzhong}</td>
                <td>${guangGaoArr[i].cishu}</td>
                <td>${guangGaoArr[i].shifouqiyong}</td>
                <td>
                    <button data-id="${guangGaoArr[i].id}" type="button" class="layui-btn layui-btn-sm changebtn"><i class="layui-icon"></i></button>
                    <button data-id="${guangGaoArr[i].id}" type="button" class="layui-btn layui-btn-normal layui-btn-sm delbtn" style="background-color: red;"><i class="layui-icon"></i></button>
                </td>
            </tr>`)
    }
}
//增加用户函数
function adduser(){
    let id = $("#ID").val();
    let guanggaomingcheng = $("#gGname").val();
    let guanggaoquanzhong = $("#weight").val();
    let cishu = $("#number").val();
    let shifouqiyong = $("input[type='radio']:checked").val();
    guangGaoArr.push({
        id:id,guanggaomingcheng:guanggaomingcheng,guanggaoquanzhong:guanggaoquanzhong,cishu:cishu,shifouqiyong:shifouqiyong
    })
}
//给添加用户按钮绑定事件
$('#addbtn').click(function(){
    let pageSize = 5; //每页显示条数
    let TotalPage = Math.ceil(guangGaoArr.length / pageSize);
    for(let i=0;i<TotalPage;i++){
        let Addstorea=$(".pageBut").eq(i).attr("class");
        let Addas=Addstorea.split(" ");
        if(Addas[1]=="cyan") {
           var pagesname= $(".pageBut").eq(i).html();
        }
    //    console.log(Addas); 
    }
    $('.modalAdd').show();
    $('#show').css('display','none');
    $('#tijiao').off('click');
    $('#tijiao').click(function(){
        layer.confirm('确认提交？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            $('.modalAdd').hide();
            $('#show').css('display','block');
            adduser();
            producePage(guangGaoArr);
            pages(guangGaoArr,pageSize,pagesname);
            layer.msg('提交成功', {icon: 1,time:500});
            });
    })
    $('#quxiao').click(function(){
        $('.modalAdd').hide();
        $('#show').css('display','block');
    })
    $('#fanhui').click(function(){
        $('.modalAdd').hide();
        $('#show').css('display','block');
    })
})
//删除用户函数
function delUserById(id) {
    for(var i=0;i<guangGaoArr.length;i++){
        if(guangGaoArr[i].id == id){
            guangGaoArr.splice(i,1);
            break;
        }
    }
}
//给删除按钮绑定事件
$('#content').on('click','.delbtn',function(){
    let pageSize = 5; //每页显示条数
    let TotalPage = Math.ceil(guangGaoArr.length / pageSize);
    for(let i=0;i<TotalPage;i++){
        let Addstorea=$(".pageBut").eq(i).attr("class");
        let Addas=Addstorea.split(" ");
        if(Addas[1]=="cyan") {
           var pagesname= $(".pageBut").eq(i).html();
        }
    //    console.log(Addas); 
    }
    let id=  $(this)[0].dataset.id;
    layer.confirm('确认删除？', {
        btn: ['确认','取消'] //按钮
    }, function(){
        delUserById(id);
        producePage(guangGaoArr);
        pages(guangGaoArr,pageSize,pagesname);
    layer.msg('已删除', {icon: 1,time:500});
    });
})
//修改用户函数
function changeuser(id){
    let name = $("#gGname1").val();
    let weight = $("#weight1").val();
    let number = $("#number1").val();
    let shifouqiyong = $("input[type='radio']:checked").val();
    let idArr = guangGaoArr.filter(function(item){
        return item.id==id;
    });
    idArr[0].guanggaomingcheng = name;
    idArr[0].guanggaoquanzhong = weight;
    idArr[0].cishu = number;
    idArr[0].shifouqiyong = shifouqiyong;
}
function changeshow(id){
    let idArr = guangGaoArr.filter(function(item){
        return item.id==id;
    });
    $("#id1").val(idArr[0].id);
    $("#gGname1").val(idArr[0].guanggaomingcheng);
    $("#weight1").val(idArr[0].guanggaoquanzhong);
    $("#number1").val(idArr[0].cishu);
}
//修改按钮绑定事件
$('#content').on('click','.changebtn',function(){
    let id=  $(this)[0].dataset.id;
    changeshow(id);
    $('.modalUpdate').show();
    $('#show').css('display','none');
    $('#tijiao1').off('click');
    $('#tijiao1').click(function(){
        let pageSize = 5; //每页显示条数
        let TotalPage = Math.ceil(guangGaoArr.length / pageSize);
        for(let i=0;i<TotalPage;i++){
            let Addstorea=$(".pageBut").eq(i).attr("class");
            let Addas=Addstorea.split(" ");
            if(Addas[1]=="cyan") {
            var pagesname= $(".pageBut").eq(i).html();
            }
        //    console.log(Addas); 
        }
        layer.confirm('确认提交？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            $('.modalUpdate').hide();
            $('#show').css('display','block');
            changeuser(id);
            pages(guangGaoArr,pageSize,pagesname);
        layer.msg('提交成功', {icon: 1,time:500});
        });
    })
    $('#quxiao1').click(function(){
        $('.modalUpdate').hide();
        $('#show').css('display','block');
    })
    $('#fanhui1').click(function(){
        $('.modalUpdate').hide();
        $('#show').css('display','block');
    })
})
//批量删除
$('#delMuch').click(function(){
    let pageSize = 5; //每页显示条数
    let TotalPage = Math.ceil(guangGaoArr.length / pageSize);
    for(let i=0;i<TotalPage;i++){
        let Addstorea=$(".pageBut").eq(i).attr("class");
        let Addas=Addstorea.split(" ");
        if(Addas[1]=="cyan") {
           var pagesname= $(".pageBut").eq(i).html();
        }
    //    console.log(Addas); 
    }
    layer.confirm('确认删除？', {
        btn: ['确认','取消'] //按钮
    }, function(){
        for(let j=0;j<$('.choose:checked').length;j++){
            let thisIndex = $('.choose:checked')[j].dataset.id;
            guangGaoArr = guangGaoArr.filter(function(item){
                return item.id !=thisIndex;
            })
        }
        producePage(guangGaoArr);
        pages(guangGaoArr,pageSize,pagesname);
    layer.msg('已删除', {icon: 1,time:500});
    });
})
//全选
$('#allcheck').click(function(){
    $('#content input[type=checkbox]').prop('checked',$(this).prop('checked'));
})
$('#content').on('click','input[type=checkbox]',function(){
    if($('#content input[type=checkbox]').length==$('#content input[type=checkbox]:checked').length){
        $('#allcheck').prop('checked',true);
    }else{
        $('#allcheck').prop('checked',false);
    }
})
//生成页码
function producePage(guangGaoArr){
    let pageSize = 5;
    let currentPage = 1;
    let totalPage = Math.ceil(guangGaoArr.length/pageSize);   //总页数等于总条数/每页的条数
    currentPage = currentPage > totalPage?totalPage : currentPage ;   //当前页
    //生成对应的页码
    $("#test1").html("");   //先将页码清空
    for(var i=1;i<=totalPage;i++){
        $("#test1").append(`
            <button class="pageBut">${i}</button>
        `)
    }
    $('.pageBut').eq(0).addClass("cyan").siblings().removeClass("cyan");
    pages(guangGaoArr,pageSize,currentPage);
}
addPageEfent();
//给页码按钮添加事件
function addPageEfent(){
    $("#piLiangAndFenYe").on("click",".pageBut",function(){
        currentPage = parseInt($(this).text());   
        // producePage(guangGaoArr);   
        $(this).addClass("cyan").siblings().removeClass("cyan");
        console.log(currentPage);
        pages(guangGaoArr,pageSize,currentPage);    
    })
}
//点击上下页
function pagenumerevent(pagenuber) {
    let pageSize = 5; //每页显示条数
    let totalPage = Math.ceil(guangGaoArr.length / pageSize);
    for(let i=0;i<totalPage;i++){
        let Addstorea=$(".pageBut").eq(i).attr("class");
        let Addas=Addstorea.split(" ");
        if(Addas[1]=="cyan") {
           var currentPage= $(".pageBut").eq(i).html();
        }
    //    console.log(Addas); 
    }
    $(pagenuber).click(function() {
        if (pagenuber == '#pagDow') {
            if (currentPage <= 1) {
                currentPage = 1;
                $('.pageBut').eq(0).addClass("cyan").siblings().removeClass("cyan");
            }else{
                currentPage--;
            }
            console.log(currentPage);
            for (let i = 0; i < totalPage; i++) {
                let buttoncontent = parseInt($('.pageBut').eq(i).text());
                if (buttoncontent == currentPage) {
                    $('.pageBut').eq(i).addClass("cyan").siblings().removeClass("cyan");
                }
            }
            pages(guangGaoArr,pageSize,currentPage);
        } else if (pagenuber == '#pagUp') {
            let totalPage = Math.ceil(guangGaoArr.length / pageSize);

            if (currentPage == totalPage) {
                currentPage = totalPage;
                console.log(currentPage);
            }else{
                currentPage++;
            }
            for (let i = 0; i < totalPage; i++) {
                let buttoncontent = parseInt($('.pageBut').eq(i).text());
                if (buttoncontent == currentPage) {
                    $('.pageBut').eq(i).addClass("cyan").siblings().removeClass("cyan");
                }
            }
        }
        pages(guangGaoArr,pageSize,currentPage);
    })
}
//点击页码显示当前页内容
// function AddPageEvent() {
//     $("#pagediv").on("click","button",function () {
//         currentPage = parseInt($(this).text());
//         $(this).addClass("cyan").siblings().removeClass("cyan");
//         page(guangGaoArr);
//     })
// }
//鼠标移入表格行改变颜色
$('#content').on('mouseover','.line',function(){
    $(this).css('background-color','#e8e8e8');
    $('#content').on('mouseout','.line',function(){
        $(this).css('background-color','white');
    })
})